/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using log4net.Config;
using NMock2;
using NUnit.Framework;
using OpenServers.Net.Common;
using OpenServers.Net.Common.FileSystem;
using OpenServers.Net.Common.Listener;
using OpenServers.Net.Common.Security;
using OpenServers.Net.Common.State;
using OpenServers.Net.Common.System;
using OpenServers.Net.Common.User;
using OpenServers.Net.Common.Utils;
using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    public class AbstractCommandTest<T> : BaseNMockTest where T : BaseCommand, new() {
        private PrivateObject<T> _privateObject;
        private MockSystem _mockSystem;
        private IFtpServer _mockFtpServer;
        private FtpState _state;
        private IListener _mockListener;
        private IFileSystem _mockFileSystem;
        private ISecurityProvider _mockSecurityProvider;
        private IUser _mockUser;
        private IMessages _mockMessages;

        public PrivateObject<T> PrivateObject {
            get { return _privateObject; }
        }

        public MockSystem MockSystem {
            get { return _mockSystem; }
        }

        public IFtpServer MockFtpServer {
            get { return _mockFtpServer; }
        }

        public FtpState State {
            get { return _state; }
        }

        public IFileSystem MockFileSystem {
            get { return _mockFileSystem; }
        }

        public ISecurityProvider MockSecurityProvider {
            get { return _mockSecurityProvider; }
        }

        public IUser MockUser {
            get { return _mockUser; }
        }

        public IMessages MockMessages {
            get { return _mockMessages; }
        }

        [SetUp]
        public override void SetUp() {
            base.SetUp();
            BasicConfigurator.Configure();

            _mockSystem = new MockSystem();
            _mockSystem.DateTimeNow = new DateTime(1977, 10, 30, 10, 20, 30);

            _mockFtpServer = Mocks.NewMock<IFtpServer>();

            _mockListener = Mocks.NewMock<IListener>();

            _mockFileSystem = Mocks.NewMock<IFileSystem>();

            _mockSecurityProvider = Mocks.NewMock<ISecurityProvider>();

            _mockMessages = Mocks.NewMock<IMessages>();

            _mockUser = Mocks.NewMock<IUser>();

            _state = new FtpState(_mockListener, null);
            _state.User = _mockUser;

            _privateObject = new PrivateObject<T>(new T());

            Expect.Between(0, int.MaxValue).On(_mockFtpServer).GetProperty("FileSystem").Will(Return.Value(MockFileSystem));
            Expect.Between(0, int.MaxValue).On(_mockFtpServer).GetProperty("SecurityProvider").Will(Return.Value(MockSecurityProvider));
            Expect.Between(0, int.MaxValue).On(_mockFtpServer).GetProperty("Messages").Will(Return.Value(MockMessages));
            Expect.Between(0, int.MaxValue).On(_mockListener).Method("Send");
        }

        protected virtual void DataReceived(IState state, byte[] buffer, int received) {
        }

        protected virtual void ConnectionOpened(IState state) {
        }
    }
}
